import pathlib
import uuid
from  django.contrib.auth import get_user_model
from django.db import models
# 自定义上传图片文件名
def generic_filename(instance, filename):
    # 获取原始图片后缀
    suffix=pathlib.Path(filename).suffix
    # 随机生成文件名
    new_filename='avatars/'+uuid.uuid4().hex+suffix
    return new_filename
def carousel_filename(instance, filename):
    # 获取原始图片后缀
    suffix=pathlib.Path(filename).suffix
    # 随机生成文件名
    new_filename='carousel/'+uuid.uuid4().hex+suffix
    return new_filename
# 动态获取用户表模型类
User=get_user_model()
# Create your models here.

# 用户附加表模型类
class UserProfile(models.Model):
    # 设置外键关联
    user = models.OneToOneField(User, on_delete=models.CASCADE,related_name='profile',verbose_name='用户主表')
    # 配置文件中媒体文件的存放目录
    avatar=models.ImageField(upload_to=generic_filename,verbose_name='头像')
    # avatar=models.CharField(max_length=100,verbose_name='头像')
    # blank=True: 客户端可以不传递该字段需要的参数值
    phone=models.CharField(max_length=11,blank=True,verbose_name='手机号码')
    points = models.IntegerField(default=0, verbose_name='个人积分')
    class Meta:
        db_table='user_profile'
# 问题分类数据表
class Category(models.Model):
    pid=models.IntegerField(null=False,verbose_name='父级分类id')
    name = models.CharField(max_length=20,null=False, verbose_name='分类名称')
    status=models.BooleanField(default=1,verbose_name='分类状态')
    level=models.IntegerField(verbose_name='分类层级')
    path = models.CharField(max_length=255,verbose_name='分类完整路径')
    class Meta:
        db_table='category'
# 问题数据表
class Question(models.Model):
    # 设置外键关联
    cate=models.ForeignKey(Category,on_delete=models.RESTRICT)
    user=models.ForeignKey(User,on_delete=models.RESTRICT)
    uuid = models.CharField(default=uuid.uuid4,max_length=40, editable=False,blank=True,null=True,unique=True)
    title = models.CharField(max_length=50,null=False, verbose_name='提问标题')
    content = models.TextField(null=False, verbose_name='提问的内容')
    status=models.BooleanField(default=0,verbose_name='问题状态')
    visit = models.IntegerField(default=0, verbose_name='浏览量')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='发布时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    class Meta:
        db_table='question'
        # 设置排序字段和排序方式
        ordering=('-visit','-id',)

# 解答数据表模型类
class Answer(models.Model):
    # 外键关联
    que=models.ForeignKey(Question,on_delete=models.RESTRICT,verbose_name="关联提问")
    user=models.ForeignKey(User,on_delete=models.RESTRICT,verbose_name="发布者")
    uuid = models.CharField(default=uuid.uuid4,max_length=40, editable=False, blank=True, null=True, unique=True)
    # profile = models.ForeignKey(UserProfile,db_column='user_id',blank=True, on_delete=models.RESTRICT, verbose_name="发布者")
    answer=models.TextField(verbose_name='解答详情')
    create_time=models.DateTimeField(auto_now_add=True,verbose_name='发布时间')
    update_time=models.DateTimeField(auto_now=True,verbose_name="更新时间")
    visit = models.IntegerField(default=0, verbose_name="阅读次数")
    star=models.IntegerField(default=0,verbose_name="推荐指数")
    status=models.BooleanField(default=True,verbose_name="状态")

    class Meta:
        db_table='answer'
        # 设置排序字段
        ordering=('star','-id')

# 验证码数据表模型类
class VerifyCode(models.Model):
    phone = models.CharField(max_length=11,null=False,verbose_name='手机号码')
    # email = models.CharField(max_length=50,blank=True,verbose_name='电子邮箱')
    #  type=1: 表示手机短信验证码
    type=models.SmallIntegerField(default=1,verbose_name='类型')
    content=models.CharField(blank=True,max_length=20,verbose_name="验证码内容")
    create_time=models.DateTimeField(auto_now_add=True,verbose_name='生成时间')
    expires=models.DateTimeField(verbose_name='过期时间')
    status=models.BooleanField(default=0,verbose_name="状态")

    class Meta:
        db_table='verify_code'


# 轮播图数据表
class Carousel(models.Model):
    page_choices=[('home','首页'),('list','列表页'),('detail','详情页'),('ucenter','用户中心')]
    page = models.CharField(max_length=50,choices=page_choices,default='home',null=False, verbose_name='展示位置')
    title = models.CharField(max_length=50,null=False,verbose_name='标题')
    url=models.ImageField(upload_to=carousel_filename,verbose_name="图片")
    link = models.CharField(max_length=100,blank=True, null=False, verbose_name='链接地址')
    order = models.SmallIntegerField(default=1, verbose_name='排序')
    status=models.BooleanField(default=1,verbose_name="状态")
    class Meta:
        db_table='carousel'


